using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class InputVCodeControl : UserControl
	{
		public delegate void dismissWithClickBtn(object sender, int nClickIndex);
		private static InputVCodeControl sInputVCodeControl;
		public string mStrAccount;
		public string mStrPass;
		public string mStrImgSid;
		private bool mBIsWrongCodeStage;
		private byte[] mImageBuf;
		private string mStrPwdMD51;
		private string mStrPwdMD52;
		private ModeDialogHelper mDlgHelper;
		private bool mBIsLoaded;
		private EventWatcher mWatcher;
		internal Grid LayoutRoot;
		internal StackPanel panelRoot;
		internal TextBlock txtTitle;
		internal TextBlock txtContent;
		internal Image imgVCode;
		internal Button btnResetImg;
		internal TextBox txtInVcode;
		internal Button btnOk;
		internal Button btnCancel;
		private bool _contentLoaded;
		public event InputVCodeControl.dismissWithClickBtn mBtnClick;
		public event TextChangedEventHandler mTxtInTextChangedHandler;
		private InputVCodeControl(bool isEnableBackKeyPress, BaseEventArgs args, string strAccout = null, string strPassWord = null, InputVCodeControl.dismissWithClickBtn clickHandler = null, TextChangedEventHandler textChangedHandler = null)
		{
			this.InitializeComponent();
			this.mDlgHelper = new ModeDialogHelper(this, isEnableBackKeyPress, (SolidColorBrush)Application.get_Current().get_Resources().get_Item("NearOpacityBrush1"), false);
			if (args != null)
			{
				this.procVerifyCodeArgs(args);
			}
			Account curAccount = AccountMgr.getCurAccount();
			if (ToolFunction.isEmptyString(strAccout))
			{
				this.mStrAccount = curAccount.strUsrName;
			}
			else
			{
				this.mStrAccount = strAccout;
			}
			if (ToolFunction.isEmptyString(strPassWord))
			{
				this.mStrPass = curAccount.strPwd;
				this.mStrPwdMD51 = curAccount.strPwd;
				this.mStrPwdMD52 = curAccount.strPwd2;
			}
			else
			{
				this.mStrPass = strPassWord;
				this.mStrPwdMD51 = Util.getCutPasswordMD5(this.mStrPass);
				this.mStrPwdMD52 = Util.getFullPasswordMD5(this.mStrPass);
			}
			this.mBtnClick = clickHandler;
			this.mTxtInTextChangedHandler = textChangedHandler;
			base.add_Loaded(new RoutedEventHandler(this.InputVCodeControl_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.InputVCodeControl_Unloaded));
		}
		private void InputVCodeControl_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.mBIsLoaded)
			{
				return;
			}
			this.mBIsLoaded = true;
			this.resetControl();
			this.showVCodeImage();
			this.registerEvent();
		}
		private void InputVCodeControl_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		public static InputVCodeControl Show(bool isEnableBackKeyPress, BaseEventArgs args, string strAccout = null, string strPassWord = null, InputVCodeControl.dismissWithClickBtn clickHandler = null, TextChangedEventHandler textChangedHandler = null)
		{
			if (InputVCodeControl.sInputVCodeControl == null)
			{
				InputVCodeControl.sInputVCodeControl = new InputVCodeControl(isEnableBackKeyPress, args, strAccout, strPassWord, clickHandler, textChangedHandler);
			}
			InputVCodeControl.sInputVCodeControl.mDlgHelper.Show();
			return InputVCodeControl.sInputVCodeControl;
		}
		public static void SetInputVCodeBoxWrongStage(BaseEventArgs args)
		{
			if (InputVCodeControl.sInputVCodeControl == null)
			{
				return;
			}
			InputVCodeControl.sInputVCodeControl._setInputVCodeBoxWrongStage();
			InputVCodeControl.sInputVCodeControl.procVerifyCodeArgs(args);
			InputVCodeControl.sInputVCodeControl.showVCodeImage();
		}
		public static void DismissInputVcodeBox()
		{
			if (InputVCodeControl.sInputVCodeControl == null)
			{
				return;
			}
			InputVCodeControl.sInputVCodeControl.dismissInputVcodeBox();
			InputVCodeControl.sInputVCodeControl = null;
		}
		public static void HideInputVcodeBox()
		{
			if (InputVCodeControl.sInputVCodeControl == null)
			{
				return;
			}
			InputVCodeControl.sInputVCodeControl.mDlgHelper.Hide(null);
		}
		public static void ShowInputVcodeBox()
		{
			if (InputVCodeControl.sInputVCodeControl == null)
			{
				return;
			}
			InputVCodeControl.sInputVCodeControl.mDlgHelper.Show();
		}
		public static bool HasIncetance()
		{
			return InputVCodeControl.sInputVCodeControl != null;
		}
		public static bool IsShown()
		{
			return InputVCodeControl.sInputVCodeControl != null && InputVCodeControl.sInputVCodeControl.mDlgHelper.IsShow();
		}
		private void registerEvent()
		{
			if (this.mWatcher == null)
			{
				this.mWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.sceneEventHandler));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_AUTH_GETVERIFYIMGE_SUCCESS, this.mWatcher);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_AUTH_GETVERIFYIMGE_ERR, this.mWatcher);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_AUTH_GETVERIFYIMGE_SUCCESS, this.mWatcher);
				EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_AUTH_GETVERIFYIMGE_ERR, this.mWatcher);
				this.mWatcher = null;
			}
		}
		private void sceneEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (EventConst.ON_NETSCENE_AUTH_GETVERIFYIMGE_SUCCESS == evtArgs.mEventID)
			{
				this.procVerifyCodeArgs(evtArgs);
				this.resetControl();
				this.showVCodeImage();
				return;
			}
		}
		private void procVerifyCodeArgs(BaseEventArgs evtArgs)
		{
			if (!evtArgs.checkType<VerifyCodeArgs>())
			{
				return;
			}
			VerifyCodeArgs verifyCodeArgs = evtArgs.mObject as VerifyCodeArgs;
			this.mImageBuf = verifyCodeArgs.mImageBuf;
			this.mStrImgSid = verifyCodeArgs.mImageSid;
		}
		private void showVCodeImage()
		{
			if (this.mImageBuf == null)
			{
				return;
			}
			if (this.mImageBuf != null)
			{
				BitmapImage bitmapImage = new BitmapImage();
				MemoryStream source = new MemoryStream(this.mImageBuf);
				try
				{
					bitmapImage.SetSource(source);
					this.imgVCode.set_Source(bitmapImage);
					return;
				}
				catch (Exception ex)
				{
					string text = ex.ToString();
					Log.smethod_5(base.GetType().ToString(), "bad mImageBuf" + text);
					return;
				}
			}
			Log.smethod_5(base.GetType().ToString(), "null == mImageBuf");
		}
		public void resetControl()
		{
			this.txtTitle.set_Text(strings.Login_Vcode_TxtIn1);
			this.txtContent.set_Text(strings.Login_Vcode_TxtHint);
			this.txtInVcode.set_Text("");
			this.txtInVcode.set_Visibility(0);
			this.btnCancel.set_Visibility(0);
			this.btnResetImg.set_Visibility(0);
			this.imgVCode.set_Visibility(0);
		}
		private void _setInputVCodeBoxWrongStage()
		{
			this.mBIsWrongCodeStage = true;
			this.txtTitle.set_Text(strings.Login_Vcode_TxtBadV);
			this.txtContent.set_Text(strings.Login_Vcode_TxtBadV2);
			this.btnCancel.set_Visibility(1);
			this.btnResetImg.set_Visibility(1);
			this.txtInVcode.set_Visibility(1);
			this.imgVCode.set_Visibility(1);
		}
		private void dismissInputVcodeBox()
		{
			this.unRegisterEvent();
			this.mDlgHelper.Hide(null);
		}
		private void btnOk_Click(object sender, RoutedEventArgs e)
		{
			if (this.mBIsWrongCodeStage)
			{
				this.resetControl();
				this.mBIsWrongCodeStage = false;
				return;
			}
			Log.smethod_5(base.GetType().ToString(), "mStrImgSid:" + this.mStrImgSid);
			ServiceCenter.sceneAuth.doSceneWithVerify(this.mStrAccount, this.mStrPwdMD51, this.mStrPwdMD52, this.mStrImgSid, this.txtInVcode.get_Text());
		}
		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			InputVCodeControl.HideInputVcodeBox();
		}
		private void btnResetImg_Click(object sender, RoutedEventArgs e)
		{
			ServiceCenter.sceneGetVerifyImage.doScene(this.mStrAccount, this.mStrPwdMD51);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/LoginViews/InputVCodeControl.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelRoot = (StackPanel)base.FindName("panelRoot");
			this.txtTitle = (TextBlock)base.FindName("txtTitle");
			this.txtContent = (TextBlock)base.FindName("txtContent");
			this.imgVCode = (Image)base.FindName("imgVCode");
			this.btnResetImg = (Button)base.FindName("btnResetImg");
			this.txtInVcode = (TextBox)base.FindName("txtInVcode");
			this.btnOk = (Button)base.FindName("btnOk");
			this.btnCancel = (Button)base.FindName("btnCancel");
		}
	}
}
